<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>prioritypool_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_structs_prioritypool_test.js.html">prioritypool_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.structs.PriorityPoolTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.structs.PriorityPoolTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.structs.PriorityPool&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>
<a name="line22"></a>// Implementation of the Pool class with isObjectDead() always returning TRUE,
<a name="line23"></a>// so that the the Pool will not reuse any objects.
<a name="line24"></a>function NoObjectReusePriorityPool(opt_min, opt_max) {
<a name="line25"></a>  goog.structs.PriorityPool.call(this, opt_min, opt_max);
<a name="line26"></a>}
<a name="line27"></a>goog.inherits(NoObjectReusePriorityPool, goog.structs.PriorityPool);
<a name="line28"></a>
<a name="line29"></a>NoObjectReusePriorityPool.prototype.objectCanBeReused = function(obj) {
<a name="line30"></a>  return false;
<a name="line31"></a>};
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>function testExceedMax1() {
<a name="line35"></a>  var p = new goog.structs.PriorityPool(0, 3);
<a name="line36"></a>
<a name="line37"></a>  var getCount1 = 0;
<a name="line38"></a>  var callback1 = function(obj) {
<a name="line39"></a>    assertNotNull(obj);
<a name="line40"></a>    getCount1++;
<a name="line41"></a>  };
<a name="line42"></a>
<a name="line43"></a>  var getCount2 = 0;
<a name="line44"></a>  var callback2 = function(obj) {
<a name="line45"></a>    getCount2++;
<a name="line46"></a>  };
<a name="line47"></a>
<a name="line48"></a>  p.getObject(callback1);
<a name="line49"></a>  p.getObject(callback1);
<a name="line50"></a>  p.getObject(callback1);
<a name="line51"></a>  p.getObject(callback2);
<a name="line52"></a>  p.getObject(callback2);
<a name="line53"></a>  p.getObject(callback2);
<a name="line54"></a>
<a name="line55"></a>  assertEquals(&#39;getCount for allocated, Should be 3&#39;, getCount1, 3);
<a name="line56"></a>  assertEquals(&#39;getCount for unallocated, Should be 0&#39;, getCount2, 0);
<a name="line57"></a>}
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>function testExceedMax2() {
<a name="line61"></a>  var p = new goog.structs.PriorityPool(0, 1);
<a name="line62"></a>
<a name="line63"></a>  var getCount1 = 0;
<a name="line64"></a>  var callback1 = function(obj) {
<a name="line65"></a>    assertNotNull(obj);
<a name="line66"></a>    getCount1++;
<a name="line67"></a>  };
<a name="line68"></a>
<a name="line69"></a>  var getCount2 = 0;
<a name="line70"></a>  var callback2 = function(obj) {
<a name="line71"></a>    getCount2++;
<a name="line72"></a>  };
<a name="line73"></a>
<a name="line74"></a>  p.getObject(callback1);
<a name="line75"></a>  p.getObject(callback2);
<a name="line76"></a>  p.getObject(callback2);
<a name="line77"></a>  p.getObject(callback2);
<a name="line78"></a>  p.getObject(callback2);
<a name="line79"></a>  p.getObject(callback2);
<a name="line80"></a>
<a name="line81"></a>  assertEquals(&#39;getCount for allocated, Should be 1&#39;, getCount1, 1);
<a name="line82"></a>  assertEquals(&#39;getCount for unallocated, Should be 0&#39;, getCount2, 0);
<a name="line83"></a>}
<a name="line84"></a>
<a name="line85"></a>function testExceedMax3() {
<a name="line86"></a>  var p = new goog.structs.PriorityPool(0, 2);
<a name="line87"></a>
<a name="line88"></a>  var obj1 = null;
<a name="line89"></a>  var callback1 = function(obj) {
<a name="line90"></a>    obj1 = obj;
<a name="line91"></a>  };
<a name="line92"></a>
<a name="line93"></a>  var obj2 = null;
<a name="line94"></a>  var callback2 = function(obj) {
<a name="line95"></a>    obj2 = obj;
<a name="line96"></a>  };
<a name="line97"></a>
<a name="line98"></a>  var obj3 = null;
<a name="line99"></a>  var callback3 = function(obj) {
<a name="line100"></a>    obj3 = obj;
<a name="line101"></a>  };
<a name="line102"></a>
<a name="line103"></a>  p.getObject(callback1);
<a name="line104"></a>  p.getObject(callback2);
<a name="line105"></a>  p.getObject(callback3);
<a name="line106"></a>
<a name="line107"></a>  assertNotNull(obj1);
<a name="line108"></a>  assertNotNull(obj2);
<a name="line109"></a>  assertNull(obj3);
<a name="line110"></a>}
<a name="line111"></a>
<a name="line112"></a>function testExceedMax4() {
<a name="line113"></a>  var p = new goog.structs.PriorityPool(); // default: 10
<a name="line114"></a>  var objs = [];
<a name="line115"></a>
<a name="line116"></a>  var getCount1 = 0;
<a name="line117"></a>  var callback1 = function(obj) {
<a name="line118"></a>    assertNotNull(obj);
<a name="line119"></a>    getCount1++;
<a name="line120"></a>  };
<a name="line121"></a>
<a name="line122"></a>  var getCount2 = 0;
<a name="line123"></a>  var callback2 = function(obj) {
<a name="line124"></a>    getCount2++;
<a name="line125"></a>  };
<a name="line126"></a>
<a name="line127"></a>  for (var i = 0; i &lt; 12; i++) {
<a name="line128"></a>    p.getObject(i &lt; 10 ? callback1 : callback2);
<a name="line129"></a>  }
<a name="line130"></a>
<a name="line131"></a>  assertEquals(&#39;getCount for allocated, Should be 10&#39;, getCount1, 10);
<a name="line132"></a>  assertEquals(&#39;getCount for unallocated, Should be 0&#39;, getCount2, 0);
<a name="line133"></a>}
<a name="line134"></a>
<a name="line135"></a>
<a name="line136"></a>function testReleaseAndGet1() {
<a name="line137"></a>  var p = new goog.structs.PriorityPool(0, 10);
<a name="line138"></a>
<a name="line139"></a>  var o = null;
<a name="line140"></a>  var callback = function(obj) {
<a name="line141"></a>    o = obj;
<a name="line142"></a>  };
<a name="line143"></a>
<a name="line144"></a>  p.getObject(callback);
<a name="line145"></a>  assertEquals(1, p.getCount());
<a name="line146"></a>  assertEquals(1, p.getInUseCount());
<a name="line147"></a>  assertEquals(0, p.getFreeCount());
<a name="line148"></a>  assertTrue(&#39;Result should be true&#39;, p.releaseObject(o));
<a name="line149"></a>  assertEquals(1, p.getCount());
<a name="line150"></a>  assertEquals(0, p.getInUseCount());
<a name="line151"></a>  assertEquals(1, p.getFreeCount());
<a name="line152"></a>}
<a name="line153"></a>
<a name="line154"></a>
<a name="line155"></a>function testReleaseAndGet2() {
<a name="line156"></a>  var p = new NoObjectReusePriorityPool(0, 10);
<a name="line157"></a>
<a name="line158"></a>  var o = null;
<a name="line159"></a>  var callback = function(obj) {
<a name="line160"></a>    o = obj;
<a name="line161"></a>  };
<a name="line162"></a>
<a name="line163"></a>  p.getObject(callback);
<a name="line164"></a>  assertEquals(1, p.getCount());
<a name="line165"></a>  assertEquals(1, p.getInUseCount());
<a name="line166"></a>  assertEquals(0, p.getFreeCount());
<a name="line167"></a>  assertTrue(&#39;Result should be true&#39;, p.releaseObject(o));
<a name="line168"></a>  assertEquals(0, p.getCount());
<a name="line169"></a>  assertEquals(0, p.getInUseCount());
<a name="line170"></a>  assertEquals(0, p.getFreeCount());
<a name="line171"></a>}
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>function testReleaseAndGet3() {
<a name="line175"></a>  var p = new goog.structs.PriorityPool(0, 10);
<a name="line176"></a>  var o1 = null;
<a name="line177"></a>  var callback1 = function(obj) {
<a name="line178"></a>    o1 = obj;
<a name="line179"></a>  };
<a name="line180"></a>
<a name="line181"></a>  var o2 = null;
<a name="line182"></a>  var callback2 = function(obj) {
<a name="line183"></a>    o2 = obj;
<a name="line184"></a>  };
<a name="line185"></a>
<a name="line186"></a>  var o3 = null;
<a name="line187"></a>  var callback3 = function(obj) {
<a name="line188"></a>    o3 = obj;
<a name="line189"></a>  };
<a name="line190"></a>
<a name="line191"></a>  var o4 = {};
<a name="line192"></a>
<a name="line193"></a>  p.getObject(callback1);
<a name="line194"></a>  p.getObject(callback2);
<a name="line195"></a>  p.getObject(callback3);
<a name="line196"></a>
<a name="line197"></a>  assertEquals(3, p.getCount());
<a name="line198"></a>  assertEquals(3, p.getInUseCount());
<a name="line199"></a>  assertEquals(0, p.getFreeCount());
<a name="line200"></a>  assertTrue(&#39;Result should be true&#39;, p.releaseObject(o1));
<a name="line201"></a>  assertTrue(&#39;Result should be true&#39;, p.releaseObject(o2));
<a name="line202"></a>  assertFalse(&#39;Result should be false&#39;, p.releaseObject(o4));
<a name="line203"></a>  assertEquals(3, p.getCount());
<a name="line204"></a>  assertEquals(1, p.getInUseCount());
<a name="line205"></a>  assertEquals(2, p.getFreeCount());
<a name="line206"></a>}
<a name="line207"></a>
<a name="line208"></a>
<a name="line209"></a>function testReleaseAndGet4() {
<a name="line210"></a>  var p = new NoObjectReusePriorityPool(0, 10);
<a name="line211"></a>
<a name="line212"></a>  var o1 = null;
<a name="line213"></a>  var callback1 = function(obj) {
<a name="line214"></a>    o1 = obj;
<a name="line215"></a>  };
<a name="line216"></a>
<a name="line217"></a>  var o2 = null;
<a name="line218"></a>  var callback2 = function(obj) {
<a name="line219"></a>    o2 = obj;
<a name="line220"></a>  };
<a name="line221"></a>
<a name="line222"></a>  var o3 = null;
<a name="line223"></a>  var callback3 = function(obj) {
<a name="line224"></a>    o3 = obj;
<a name="line225"></a>  };
<a name="line226"></a>
<a name="line227"></a>  var o4 = {};
<a name="line228"></a>
<a name="line229"></a>  p.getObject(callback1);
<a name="line230"></a>  p.getObject(callback2);
<a name="line231"></a>  p.getObject(callback3);
<a name="line232"></a>  assertEquals(3, p.getCount());
<a name="line233"></a>  assertEquals(3, p.getInUseCount());
<a name="line234"></a>  assertEquals(0, p.getFreeCount());
<a name="line235"></a>  assertTrue(&#39;Result should be true&#39;, p.releaseObject(o1));
<a name="line236"></a>  assertTrue(&#39;Result should be true&#39;, p.releaseObject(o2));
<a name="line237"></a>  assertFalse(&#39;Result should be false&#39;, p.releaseObject(o4));
<a name="line238"></a>  assertEquals(1, p.getCount());
<a name="line239"></a>  assertEquals(1, p.getInUseCount());
<a name="line240"></a>  assertEquals(0, p.getFreeCount());
<a name="line241"></a>}
<a name="line242"></a>
<a name="line243"></a>
<a name="line244"></a>function testIsInPool1() {
<a name="line245"></a>  var p = new goog.structs.PriorityPool();
<a name="line246"></a>  var o1 = null;
<a name="line247"></a>  var callback1 = function(obj) {
<a name="line248"></a>    o1 = obj;
<a name="line249"></a>  };
<a name="line250"></a>
<a name="line251"></a>  var o2 = null;
<a name="line252"></a>  var callback2 = function(obj) {
<a name="line253"></a>    o2 = obj;
<a name="line254"></a>  };
<a name="line255"></a>
<a name="line256"></a>  var o3 = null;
<a name="line257"></a>  var callback3 = function(obj) {
<a name="line258"></a>    o3 = obj;
<a name="line259"></a>  };
<a name="line260"></a>
<a name="line261"></a>  var o4 = {};
<a name="line262"></a>  var o5 = {};
<a name="line263"></a>
<a name="line264"></a>  p.getObject(callback1);
<a name="line265"></a>  p.getObject(callback2);
<a name="line266"></a>  p.getObject(callback3);
<a name="line267"></a>  var o6 = o1;
<a name="line268"></a>
<a name="line269"></a>  assertTrue(p.contains(o1));
<a name="line270"></a>  assertTrue(p.contains(o2));
<a name="line271"></a>  assertTrue(p.contains(o3));
<a name="line272"></a>  assertFalse(p.contains(o4));
<a name="line273"></a>  assertFalse(p.contains(o5));
<a name="line274"></a>  assertTrue(p.contains(o6));
<a name="line275"></a>}
<a name="line276"></a>
<a name="line277"></a>
<a name="line278"></a>function testSetMin1() {
<a name="line279"></a>  var p = new goog.structs.PriorityPool(0, 10);
<a name="line280"></a>
<a name="line281"></a>  assertEquals(0, p.getCount());
<a name="line282"></a>  assertEquals(0, p.getInUseCount());
<a name="line283"></a>  assertEquals(0, p.getFreeCount());
<a name="line284"></a>
<a name="line285"></a>  p.setMinimumCount(10);
<a name="line286"></a>
<a name="line287"></a>  assertEquals(10, p.getCount());
<a name="line288"></a>  assertEquals(0, p.getInUseCount());
<a name="line289"></a>  assertEquals(10, p.getFreeCount());
<a name="line290"></a>}
<a name="line291"></a>
<a name="line292"></a>
<a name="line293"></a>function testSetMin2() {
<a name="line294"></a>  var p = new goog.structs.PriorityPool(0, 10);
<a name="line295"></a>
<a name="line296"></a>  assertEquals(0, p.getCount());
<a name="line297"></a>  assertEquals(0, p.getInUseCount());
<a name="line298"></a>  assertEquals(0, p.getFreeCount());
<a name="line299"></a>
<a name="line300"></a>  var o1 = null;
<a name="line301"></a>  var callback1 = function(obj) {
<a name="line302"></a>    o1 = obj;
<a name="line303"></a>  };
<a name="line304"></a>  p.getObject(callback1);
<a name="line305"></a>
<a name="line306"></a>  assertEquals(1, p.getCount());
<a name="line307"></a>  assertEquals(1, p.getInUseCount());
<a name="line308"></a>  assertEquals(0, p.getFreeCount());
<a name="line309"></a>
<a name="line310"></a>  p.setMinimumCount(10);
<a name="line311"></a>
<a name="line312"></a>  assertEquals(10, p.getCount());
<a name="line313"></a>  assertEquals(1, p.getInUseCount());
<a name="line314"></a>  assertEquals(9, p.getFreeCount());
<a name="line315"></a>}
<a name="line316"></a>
<a name="line317"></a>
<a name="line318"></a>function testSetMax1() {
<a name="line319"></a>  var p = new goog.structs.PriorityPool(0, 10);
<a name="line320"></a>
<a name="line321"></a>  assertEquals(0, p.getCount());
<a name="line322"></a>  assertEquals(0, p.getInUseCount());
<a name="line323"></a>  assertEquals(0, p.getFreeCount());
<a name="line324"></a>
<a name="line325"></a>  var o1 = null;
<a name="line326"></a>  var callback1 = function(obj) {
<a name="line327"></a>    o1 = obj;
<a name="line328"></a>  };
<a name="line329"></a>
<a name="line330"></a>  var o2 = null;
<a name="line331"></a>  var callback2 = function(obj) {
<a name="line332"></a>    o2 = obj;
<a name="line333"></a>  };
<a name="line334"></a>
<a name="line335"></a>  var o3 = null;
<a name="line336"></a>  var callback3 = function(obj) {
<a name="line337"></a>    o3 = obj;
<a name="line338"></a>  };
<a name="line339"></a>
<a name="line340"></a>  var o4 = null;
<a name="line341"></a>  var callback4 = function(obj) {
<a name="line342"></a>    o4 = obj;
<a name="line343"></a>  };
<a name="line344"></a>
<a name="line345"></a>  var o5 = null;
<a name="line346"></a>  var callback5 = function(obj) {
<a name="line347"></a>    o5 = obj;
<a name="line348"></a>  };
<a name="line349"></a>
<a name="line350"></a>  p.getObject(callback1);
<a name="line351"></a>  p.getObject(callback2);
<a name="line352"></a>  p.getObject(callback3);
<a name="line353"></a>  p.getObject(callback4);
<a name="line354"></a>  p.getObject(callback5);
<a name="line355"></a>
<a name="line356"></a>  assertEquals(5, p.getCount());
<a name="line357"></a>  assertEquals(5, p.getInUseCount());
<a name="line358"></a>  assertEquals(0, p.getFreeCount());
<a name="line359"></a>
<a name="line360"></a>  assertTrue(&#39;Result should be true&#39;, p.releaseObject(o5));
<a name="line361"></a>
<a name="line362"></a>  assertEquals(5, p.getCount());
<a name="line363"></a>  assertEquals(4, p.getInUseCount());
<a name="line364"></a>  assertEquals(1, p.getFreeCount());
<a name="line365"></a>
<a name="line366"></a>  p.setMaximumCount(4);
<a name="line367"></a>
<a name="line368"></a>  assertEquals(4, p.getCount());
<a name="line369"></a>  assertEquals(4, p.getInUseCount());
<a name="line370"></a>  assertEquals(0, p.getFreeCount());
<a name="line371"></a>}
<a name="line372"></a>
<a name="line373"></a>
<a name="line374"></a>function testInvalidMinMax1() {
<a name="line375"></a>  var p = new goog.structs.PriorityPool(0, 10);
<a name="line376"></a>
<a name="line377"></a>  assertEquals(0, p.getCount());
<a name="line378"></a>  assertEquals(0, p.getInUseCount());
<a name="line379"></a>  assertEquals(0, p.getFreeCount());
<a name="line380"></a>
<a name="line381"></a>  assertThrows(function() {
<a name="line382"></a>    p.setMinimumCount(11);
<a name="line383"></a>  });
<a name="line384"></a>}
<a name="line385"></a>
<a name="line386"></a>
<a name="line387"></a>function testInvalidMinMax2() {
<a name="line388"></a>  var p = new goog.structs.PriorityPool(5, 10);
<a name="line389"></a>
<a name="line390"></a>  assertEquals(5, p.getCount());
<a name="line391"></a>  assertEquals(0, p.getInUseCount());
<a name="line392"></a>  assertEquals(5, p.getFreeCount());
<a name="line393"></a>
<a name="line394"></a>  assertThrows(function() {
<a name="line395"></a>    p.setMaximumCount(4);
<a name="line396"></a>  });
<a name="line397"></a>}
<a name="line398"></a>
<a name="line399"></a>
<a name="line400"></a>function testInvalidMinMax3() {
<a name="line401"></a>  assertThrows(function() {
<a name="line402"></a>    new goog.structs.PriorityPool(10, 1);
<a name="line403"></a>  });
<a name="line404"></a>}
<a name="line405"></a>
<a name="line406"></a>function testQueue1() {
<a name="line407"></a>  var p = new goog.structs.PriorityPool(0, 2);
<a name="line408"></a>
<a name="line409"></a>  var o1 = null;
<a name="line410"></a>  var callback1 = function(obj) {
<a name="line411"></a>    o1 = obj;
<a name="line412"></a>  };
<a name="line413"></a>
<a name="line414"></a>  var o2 = null;
<a name="line415"></a>  var callback2 = function(obj) {
<a name="line416"></a>    o2 = obj;
<a name="line417"></a>  };
<a name="line418"></a>
<a name="line419"></a>  var o3 = null;
<a name="line420"></a>  var callback3 = function(obj) {
<a name="line421"></a>    o3 = obj;
<a name="line422"></a>  };
<a name="line423"></a>
<a name="line424"></a>  p.getObject(callback1);
<a name="line425"></a>  p.getObject(callback2);
<a name="line426"></a>  p.getObject(callback3);
<a name="line427"></a>
<a name="line428"></a>  assertNotNull(o1);
<a name="line429"></a>  assertNotNull(o2);
<a name="line430"></a>  assertNull(o3);
<a name="line431"></a>
<a name="line432"></a>  p.releaseObject(o1);
<a name="line433"></a>  assertNotNull(o3);
<a name="line434"></a>}
<a name="line435"></a>
<a name="line436"></a>
<a name="line437"></a>function testPriority1() {
<a name="line438"></a>  var p = new goog.structs.PriorityPool(0, 2);
<a name="line439"></a>
<a name="line440"></a>  var o1 = null;
<a name="line441"></a>  var callback1 = function(obj) {
<a name="line442"></a>    o1 = obj;
<a name="line443"></a>  };
<a name="line444"></a>
<a name="line445"></a>  var o2 = null;
<a name="line446"></a>  var callback2 = function(obj) {
<a name="line447"></a>    o2 = obj;
<a name="line448"></a>  };
<a name="line449"></a>
<a name="line450"></a>  var o3 = null;
<a name="line451"></a>  var callback3 = function(obj) {
<a name="line452"></a>    o3 = obj;
<a name="line453"></a>  };
<a name="line454"></a>
<a name="line455"></a>  var o4 = null;
<a name="line456"></a>  var callback4 = function(obj) {
<a name="line457"></a>    o4 = obj;
<a name="line458"></a>  };
<a name="line459"></a>
<a name="line460"></a>  var o5 = null;
<a name="line461"></a>  var callback5 = function(obj) {
<a name="line462"></a>    o5 = obj;
<a name="line463"></a>  };
<a name="line464"></a>
<a name="line465"></a>  var o6 = null;
<a name="line466"></a>  var callback6 = function(obj) {
<a name="line467"></a>    o6 = obj;
<a name="line468"></a>  };
<a name="line469"></a>
<a name="line470"></a>  p.getObject(callback1);       // Initially seeded requests.
<a name="line471"></a>  p.getObject(callback2);
<a name="line472"></a>
<a name="line473"></a>  p.getObject(callback3, 101);  // Lowest priority.
<a name="line474"></a>  p.getObject(callback4);       // Second lowest priority (default is 100).
<a name="line475"></a>  p.getObject(callback5, 99);   // Second highest priority.
<a name="line476"></a>  p.getObject(callback6, 0);    // Highest priority.
<a name="line477"></a>
<a name="line478"></a>  assertNotNull(o1);
<a name="line479"></a>  assertNotNull(o2);
<a name="line480"></a>  assertNull(o3);
<a name="line481"></a>  assertNull(o4);
<a name="line482"></a>  assertNull(o5);
<a name="line483"></a>  assertNull(o6);
<a name="line484"></a>
<a name="line485"></a>  p.releaseObject(o1);  // Release the first initially seeded request (o1).
<a name="line486"></a>  assertNotNull(o6);    // Make sure the highest priority request (o6) started.
<a name="line487"></a>  assertNull(o3);
<a name="line488"></a>  assertNull(o4);
<a name="line489"></a>  assertNull(o5);
<a name="line490"></a>
<a name="line491"></a>  p.releaseObject(o2);  // Release the second, initially seeded request (o2).
<a name="line492"></a>  assertNotNull(o5);    // The second highest priority request starts (o5).
<a name="line493"></a>  assertNull(o3);
<a name="line494"></a>  assertNull(o4);
<a name="line495"></a>
<a name="line496"></a>  p.releaseObject(o6);
<a name="line497"></a>  assertNotNull(o4);
<a name="line498"></a>  assertNull(o3);
<a name="line499"></a>}
<a name="line500"></a>
<a name="line501"></a>
<a name="line502"></a>function testRateLimiting() {
<a name="line503"></a>  var clock = new goog.testing.MockClock();
<a name="line504"></a>  clock.install();
<a name="line505"></a>
<a name="line506"></a>  var p = new goog.structs.PriorityPool(0, 4);
<a name="line507"></a>  p.setDelay(100);
<a name="line508"></a>
<a name="line509"></a>  var getCount = 0;
<a name="line510"></a>  var callback = function(obj) {
<a name="line511"></a>    assertNotNull(obj);
<a name="line512"></a>    getCount++;
<a name="line513"></a>  };
<a name="line514"></a>
<a name="line515"></a>  p.getObject(callback);
<a name="line516"></a>  assertEquals(1, getCount);
<a name="line517"></a>
<a name="line518"></a>  p.getObject(callback);
<a name="line519"></a>  assertEquals(1, getCount);
<a name="line520"></a>
<a name="line521"></a>  clock.tick(100);
<a name="line522"></a>  assertEquals(2, getCount);
<a name="line523"></a>
<a name="line524"></a>  p.getObject(callback);
<a name="line525"></a>  p.getObject(callback);
<a name="line526"></a>  assertEquals(2, getCount);
<a name="line527"></a>
<a name="line528"></a>  clock.tick(100);
<a name="line529"></a>  assertEquals(3, getCount);
<a name="line530"></a>
<a name="line531"></a>  clock.tick(100);
<a name="line532"></a>  assertEquals(4, getCount);
<a name="line533"></a>
<a name="line534"></a>  p.getObject(callback);
<a name="line535"></a>  assertEquals(4, getCount);
<a name="line536"></a>
<a name="line537"></a>  clock.tick(100);
<a name="line538"></a>  assertEquals(4, getCount);
<a name="line539"></a>
<a name="line540"></a>  goog.dispose(clock);
<a name="line541"></a>}
<a name="line542"></a>
<a name="line543"></a>
<a name="line544"></a>function testRateLimitingWithChangingDelay() {
<a name="line545"></a>  var clock = new goog.testing.MockClock();
<a name="line546"></a>  clock.install();
<a name="line547"></a>
<a name="line548"></a>  var p = new goog.structs.PriorityPool(0, 3);
<a name="line549"></a>  p.setDelay(100);
<a name="line550"></a>
<a name="line551"></a>  var getCount = 0;
<a name="line552"></a>  var callback = function(obj) {
<a name="line553"></a>    assertNotNull(obj);
<a name="line554"></a>    getCount++;
<a name="line555"></a>  };
<a name="line556"></a>
<a name="line557"></a>  p.getObject(callback);
<a name="line558"></a>  assertEquals(1, getCount);
<a name="line559"></a>
<a name="line560"></a>  p.getObject(callback);
<a name="line561"></a>  assertEquals(1, getCount);
<a name="line562"></a>
<a name="line563"></a>  clock.tick(50);
<a name="line564"></a>  assertEquals(1, getCount);
<a name="line565"></a>
<a name="line566"></a>  p.setDelay(50);
<a name="line567"></a>  assertEquals(2, getCount);
<a name="line568"></a>
<a name="line569"></a>  p.getObject(callback);
<a name="line570"></a>  assertEquals(2, getCount);
<a name="line571"></a>
<a name="line572"></a>  clock.tick(20);
<a name="line573"></a>  assertEquals(2, getCount);
<a name="line574"></a>
<a name="line575"></a>  p.setDelay(40);
<a name="line576"></a>  assertEquals(2, getCount);
<a name="line577"></a>
<a name="line578"></a>  clock.tick(20);
<a name="line579"></a>  assertEquals(3, getCount);
<a name="line580"></a>
<a name="line581"></a>  goog.dispose(clock);
<a name="line582"></a>}
</pre>


</body>
</html>
